package com.miaoxun.utils.net;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.scheduling.annotation.Async;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * http post
 */
public class MNRequest {

    private MNRequest() {
    }

    private static MNRequest instance;

    public static  MNRequest getInstance() {
        if (instance == null)
            synchronized (MNRequest.class) {
                if (instance == null)
                    instance = new MNRequest();
            }

        return instance;
    }

@Async
    public void SendPost(String url, Map<String, String> map, IRequest request) {
        if (map == null)
            map = new HashMap<>();
        SendPost(url, map, "utf-8", request);
    }
    @Async
    public void SendPost(String url, Map<String, String> map, String charset, IRequest request) {
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try {
            httpClient = HttpClientBuilder.create().build();
            httpPost = new HttpPost(url);
            //设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            for (Map.Entry<String, String> elem : map.entrySet()) {
                list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
            }
            if (list.size() > 0) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                    request.response(result);
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }












    @Async
    public void Send(String url, Map<String, String> map, IRequest request) {
        if (map == null)
            map = new HashMap<>();
        SendGet(url, map, "utf-8", request);
    }
    @Async
    public void SendGet (String url, Map<String, String> map, String charset, IRequest request) {
        HttpClient httpClient = null;
        HttpGet httpPost = null;
        String result = null;


        try {
            httpClient = HttpClientBuilder.create().build();
            httpPost = new HttpGet(url);
            //设置参数
         StringBuilder sb=new StringBuilder(url);
            if (!url.endsWith("?"))
                sb.append("?");
            for (Map.Entry<String, String> elem : map.entrySet()) {
                 sb.append(elem.getKey()).append("=").append(elem.getValue()).append("&");
            }

            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                    request.response(result);
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }






}
